package com.sttk.secreconeclick.bookkeep.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.BaseAc;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.databinding.ActivityOptionSettingBinding;
import com.sttk.secreconeclick.bookkeep.dialog.MoneyColorDialog;

public class OptionSettingActivity extends BaseAc<ActivityOptionSettingBinding> implements View.OnClickListener {

    private int textPos, colorPos;
    private boolean isShowBalance;

    @Override
    protected int onCreate() {
        return R.layout.activity_option_setting;
    }

    @Override
    protected void initView() {
        mDataBinding.ivOptionSettingBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvExpenditure.setOnClickListener(this);
        mDataBinding.tvIncome.setOnClickListener(this);
        mDataBinding.tvBalacne.setOnClickListener(this);
        mDataBinding.ivShowBalance.setOnClickListener(this);
        mDataBinding.llMoneyColor.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvExpenditure) {//首页大字显示/支出
            clearSelection();
            mDataBinding.tvExpenditure.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvExpenditure.setBackgroundResource(R.drawable.orange_18_dp);
            textPos = 0;
            SPUtil.putInt(mContext, Content.indexText, textPos);
        } else if (id == R.id.tvIncome) {//首页大字显示/收入
            clearSelection();
            mDataBinding.tvIncome.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvIncome.setBackgroundResource(R.drawable.orange_18_dp);
            textPos = 1;
            SPUtil.putInt(mContext, Content.indexText, textPos);
        } else if (id == R.id.tvBalacne) {//首页大字显示/结余
            clearSelection();
            mDataBinding.tvBalacne.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvBalacne.setBackgroundResource(R.drawable.orange_18_dp);
            textPos = 2;
            SPUtil.putInt(mContext, Content.indexText, textPos);
        } else if (id == R.id.ivShowBalance) {//月/年周期显示总计结余
            isShowBalance = !isShowBalance;
            SPUtil.putBoolean(mContext, Content.showBalance, isShowBalance);
            mDataBinding.ivShowBalance.setImageResource(isShowBalance ? R.drawable.kai : R.drawable.guan);
        } else if (id == R.id.llMoneyColor) {//金额颜色
            showMoneyColorDialog();
        }
    }

    @Override
    protected void initData() {
        textPos = SPUtil.getInt(mContext, Content.indexText, 0);
        clearSelection();
        if (textPos == 0) {
            mDataBinding.tvExpenditure.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvExpenditure.setBackgroundResource(R.drawable.orange_18_dp);
        } else if (textPos == 1) {
            mDataBinding.tvIncome.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvIncome.setBackgroundResource(R.drawable.orange_18_dp);
        } else if (textPos == 2) {
            mDataBinding.tvBalacne.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvBalacne.setBackgroundResource(R.drawable.orange_18_dp);
        }
        colorPos = SPUtil.getInt(mContext, Content.moneyColor, 0);
        if (colorPos == 0) {
            mDataBinding.tvMoneyColor.setText(getString(R.string.money_color_text1));
        } else if (colorPos == 1) {
            mDataBinding.tvMoneyColor.setText(getString(R.string.money_color_text2));
        }
        isShowBalance = SPUtil.getBoolean(mContext, Content.showBalance, true);
        mDataBinding.ivShowBalance.setImageResource(isShowBalance ? R.drawable.kai : R.drawable.guan);
    }

    private void showMoneyColorDialog() {
        MoneyColorDialog dialog = new MoneyColorDialog(mContext);
        dialog.setListener(new MoneyColorDialog.Ilistener() {
            @Override
            public void clickItem(int pos) {
                colorPos = pos;
                SPUtil.putInt(mContext, Content.moneyColor, colorPos);
                if (colorPos == 0) {
                    mDataBinding.tvMoneyColor.setText(getString(R.string.money_color_text1));
                } else if (colorPos == 1) {
                    mDataBinding.tvMoneyColor.setText(getString(R.string.money_color_text2));
                }
            }
        });
        dialog.show();
    }

    private void clearSelection() {
        mDataBinding.tvExpenditure.setTextColor(Color.parseColor("#323433"));
        mDataBinding.tvExpenditure.setBackgroundResource(R.drawable.gray_18_dp);
        mDataBinding.tvIncome.setTextColor(Color.parseColor("#323433"));
        mDataBinding.tvIncome.setBackgroundResource(R.drawable.gray_18_dp);
        mDataBinding.tvBalacne.setTextColor(Color.parseColor("#323433"));
        mDataBinding.tvBalacne.setBackgroundResource(R.drawable.gray_18_dp);
    }

    @Override
    public FrameLayout getNativeView() {
        return mDataBinding.adContainer;
    }
}